<?php

namespace App\Admin\Controllers;

use App\Models\AddressModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AddressController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '研学地区管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AddressModel());

        $grid->column('id', __('Id'));
        $grid->column('address','地区');
        //$grid->column('parent_id', __('Parent id'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(AddressModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('address','地区');
        //$show->field('parent_id', __('Parent id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AddressModel());

        $form->select('parent_id','請選国家')
            ->options(AddressModel::where('parent_id',0)->pluck('address as title','id'))
            ->default(0)
            ->load('city_id', '/admin/city');

        $form->text('address','地区名称');

        $form->saving(function (Form $form) {
            if(empty($form->parent_id)){
                $form->parent_id =  0;
            }
        });

        return $form;
    }
}
